import 'package:api/pages/apps/firstCompanyApp/model/video_model.dart';
import 'package:flutter/material.dart';

import 'item/find_video_item_page.dart';

class MainFindPage extends StatefulWidget {
  @override
  _MainFindPageState createState() => _MainFindPageState();
}

class _MainFindPageState extends State<MainFindPage>
    with SingleTickerProviderStateMixin {
  List<String> tabTextList = ["关注", "推荐"];
  // 推荐模拟数据
  List<VideoModel> videoList = [];
  // 关注模拟数据
  List<VideoModel> videoList2 = [];
  TabController tabController;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    tabController = new TabController(length: tabTextList.length, vsync: this);
    // 创建模拟数据
    for (int i = 0; i < 10; i++) {
      VideoModel videoModel = new VideoModel();
      VideoModel videoModel2 = new VideoModel();
      videoModel.videoName = "推荐测试数据$i";
      videoModel.pariseCount = i * 22;

      videoModel2.videoName = "推荐测试数据$i";
      videoModel2.pariseCount = i * 22;

      if (i % 3 == 0) {
        videoModel2.isAttention = true;
        videoModel.isAttention = true;
        videoModel.isLike = true;
      } else {
        videoModel.isAttention = false;
        videoModel.isLike = false;
      }
      videoModel.videoUrl =
          "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-366ba130-3d1f-4284-a453-7dde238bdc9d/66c8170c-a289-45b4-90df-a2ad74847afa.mp4";
      videoModel.videoImage =
          "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-366ba130-3d1f-4284-a453-7dde238bdc9d/0b798a04-f674-4061-906d-e951887199a7.png";
      videoList.add(videoModel);

      videoModel2.videoUrl =
          "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-366ba130-3d1f-4284-a453-7dde238bdc9d/66c8170c-a289-45b4-90df-a2ad74847afa.mp4";
      videoModel2.videoImage =
          "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-366ba130-3d1f-4284-a453-7dde238bdc9d/0b798a04-f674-4061-906d-e951887199a7.png";
      videoList2.add(videoModel2);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: [
        buildBgWiget(),
        buildTabViewWidget(),
        buildTabWidget(),
      ],
    ));
  }

  buildBgWiget() {
    return Positioned(
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
        child: Container(color: Colors.black));
  }

  buildTabViewWidget() {
    return Positioned(
      left: 0,
      right: 0,
      top: 0,
      bottom: 0,
      child: TabBarView(
        controller: tabController,
        children:
            tabTextList.map((value) => buildTabViewItemWidget(value)).toList(),
      ),
    );
  }

  buildTabWidget() {
    return Positioned(
      left: 0,
      right: 0,
      top: 40,
      bottom: 0,
      child: Container(
        alignment: Alignment.topCenter,
        child: TabBar(
            // 指示器的颜色
            indicatorColor: Colors.white,
            // 指示器的高度
            indicatorWeight: 2.0,
            isScrollable: true,
            // 指示器宽度与文字对齐
            indicatorSize: TabBarIndicatorSize.label,
            controller: tabController,
            tabs: tabTextList.map((value) => Tab(text: "$value")).toList()),
      ),
    );
  }

  Widget buildTabViewItemWidget(String value) {
    List<VideoModel> list = [];
    if (value == "推荐") {
      list = videoList;
    } else {
      list = videoList2;
    }
    return PageView.builder(
        scrollDirection: Axis.vertical,
        itemCount: videoList.length,
        itemBuilder: (BuildContext context, int index) {
          VideoModel videoModel = list[index];
          return buildPageViewItemWidget(value, videoModel);
        });
  }

  Widget buildPageViewItemWidget(String value, VideoModel videoModel) {
    return FindVideoItemPage(value, videoModel);
  }
}
